
import 'package:sciolto/POJO/storable.dart';

class Song extends Storable{

  int _id;
  String _mp3FileName;

  String _songName;
  String _singerName;

  Song(this._mp3FileName){
    assert(_mp3FileName != null);
    this._songName = _mp3FileName.split("-")[1].split(".")[0];
    this._singerName = _mp3FileName.split("-")[0];
    this._id = -1;
  }

  Song.withError(){
    this._mp3FileName = "unknown - error.mp3";
    this._songName = _mp3FileName.split("-")[1].split(".")[0];
    this._singerName = _mp3FileName.split("-")[0];
    this._id = -1;
  }

  Song.fromMap(Map<String, dynamic> map){
    assert(
      map["id"] != null &&
      map["mp3_file_name"] != null
    );
    this._id = map["id"];
    this._mp3FileName = map["mp3_file_name"];
    this._songName = _mp3FileName.split("-")[1].split(".")[0];
    this._singerName = _mp3FileName.split("-")[0];
  }

  @override
  bool operator ==(Object other) =>
      identical(this, other) ||
      other is Song &&
          runtimeType == other.runtimeType &&
          _id == other._id &&
          _mp3FileName == other._mp3FileName &&
          _songName == other._songName &&
          _singerName == other._singerName;

  @override
  int get hashCode =>
      _id.hashCode ^
      _mp3FileName.hashCode ^
      _songName.hashCode ^
      _singerName.hashCode;

  String get singerName => _singerName;

  String get songName => _songName;

  String get mp3FileName => _mp3FileName;

  int get id => _id;

  @override
  Map<String, dynamic> toMap(){
    Map<String, dynamic> result = {};
    result['mp3_file_name'] = mp3FileName;
    if(id != -1) result['id'] = id;
    return result;
  }

  @override
  String toString() {
    return 'Song{_id: $_id, '
        '_mp3FileName: $_mp3FileName, '
        '_songName: $_songName, '
        '_singerName: $_singerName'
        '}';
  }
}


